package com.huixue.server.rule;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.Cookie;
import javax.xml.registry.infomodel.User;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.huixue.server.basic.Common;

public class VerifyToken {
	private static SqlSessionFactory factory = Common.getSqlSessionFactory();

	public Boolean verify(String cmd, Cookie[] cookies) {
		String userInfo = null;
		String token = null;
		String userLoginName = null;
		if (cmd.equals("50000")) {
			return true;
		}
		if (cookies == null) {
			return false;
		}
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals("user_info")) {
				try {
					userInfo = java.net.URLDecoder.decode(cookies[i].getValue()
							.trim(), "UTF-8");
					token = Common.getValueFromParam("token", userInfo);
					userLoginName = Common.getValueFromParam("user_login_name",
							userInfo);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
//		User user = new User();
//		SqlSession sqlSession = factory.openSession();
//		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//		user = userMapper.getUserByLoginName(userLoginName);
//		if (null != user && !"".equals(user) && !"null".equals(user)
//				&& token.equals(user.getToken())) {
//			return true;
//		}
		return false;
	}
}
